/**
 * DOM 操作封装
 */
// 文档加载事件:
/**
 * onload:所有资源加载完成后,才执行js代码
 * DOMContentloaded:只需要dom加载完成后,据可以执行js代码
 */

const { get } = require("./local")


function ready(callback) {
    window.addEventListener("DOMContentLoaded", () => callback())
}


function getEle(selector) {
    return document.querySelector(selector)
}

function getEleAll(selector) {
    return document.querySelectorAll(selector)
}

function addEvent(obj, callback, type = "click") {
    document.querySelector(obj).addEventListener(type, () => {
        callback()
    })
}

function isLogin() {
    if (!get('token')) {
        location.href = 'login.html'
    }
}

function publicFooter(type = "home") {
    let footer = document.createElement("footer")
    footer.innerHTML = `
        <a href="./home.html" class="${type == 'home' ? 'active' : ''}">
            <i class="iconfont icon-shouye"></i>
            <span calss='f14'>首页</span>
        </a>
        <a href="./run.html" class="${type == 'run' ? 'active' : ''} ">
            <i class="iconfont icon-xinshuai"></i>
            <span>运动</span>
        </a>
        <a href="./cricle.html" class="${type == 'cricle' ? 'active' : ''}">
            <i class="iconfont icon-pintu"></i>
            <span>圈子</span>
        </a>
        <a href="./my.html" class="${type == 'my' ? 'active' : ''}">
            <i class="iconfont icon-yonghu-yuan"></i>
            <span>我的</span>
        </a>
        
    `
    document.body.append(footer)
}

function publicHeader(type = "run") {
    let header = document.createElement("header")
    header.classList.add("myHeader")
    header.innerHTML = `
        <div class="container">
            <a href="./run.html" class="${type == "run" ? "active" : ""}">跑步</a>
            <a href="./ride.html" class="${type == "ride" ? "active" : ""}">骑行</a>
            <a href="./course.html" class="${type == "course" ? "active" : ""}">课程训练</a>
        </div>
    `
    document.body.prepend(header)
}


module.exports = {
    ready,
    getEle,
    getEleAll,
    addEvent,
    publicFooter,
    publicHeader,
    isLogin,
}